package com.ltsznh.android.familybooks.account;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.ltsznh.android.familybooks.R;
import com.ltsznh.android.familybooks.db.dbo;

public class AccountActivity extends Activity {

    private ArrayAdapter<String> accountAdapter;
    private ListView accountListView;
    private String accountCategory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category2);

        ActionBar actionBar = this.getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        accountAdapter = new ArrayAdapter<String>(this, R.layout.category);
        accountListView = (ListView)findViewById(R.id.listView_category2);
        accountListView.setAdapter(accountAdapter);

        accountCategory = getIntent().getStringExtra("accountCategory");

        ((TextView)findViewById(R.id.textView_category2_category)).setText(accountCategory);
        refreshData();

        initAccountEdit();
    }

    public void refreshData(){
        dbo.getReadableDatabase(this);
        String sqlString = "select distinct account from inv_account where accountCategory = '" + accountCategory + "' order by displayOrder";
        Cursor accountCursor = dbo.fbSQLite.rawQuery(sqlString, null);
        accountAdapter.clear();
        if(accountCursor.getCount()>0){
            accountCursor.moveToFirst();
            do{
                accountAdapter.add(accountCursor.getString(0));
            }while (accountCursor.moveToNext());
        }
        accountCursor.close();
        dbo.closeDatabase();
    }

    protected void initAccountEdit(){
        accountListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                String account = accountAdapter.getItem(i).toString();
                AccountEditDialog accountEditDialog = AccountEditDialog.newInstance(account);
                accountEditDialog.setArgs(AccountActivity.this,"inv_account","account","account = '" + account + "'");
                accountEditDialog.show(getFragmentManager(), null);
                return false;
            }
        });
    }

    protected void addAccount(){
        AccountAddDialog accountAddDialog = AccountAddDialog.newInstance(accountCategory);
        accountAddDialog.setArgs(AccountActivity.this,"inv_account","accountCategory","account");
        accountAddDialog.show(getFragmentManager(), null);
    }

    protected void goBack(){
        Intent intent = new Intent(this, AccountCategoryActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                goBack();
                return true;
            case R.id.action_add:
                addAccount();
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.category2, menu);
        return true;
    }
    
}
